package ListDemo;

import java.util.Arrays;

public class MyArrayList implements IList {
    public int []elem;
    //有效数字个数
    public int usedSize = 0;

    public MyArrayList() {
        this.elem = new int[10];
    }

    @Override
    public Boolean isFull() {
        return usedSize == elem.length;
    }

    @Override
    public void add(int data) {
        if(isFull()) {
            resize();
        }
        elem[usedSize] = data;
        usedSize++;
    }
    private void resize() {
        elem = Arrays.copyOf(elem,elem.length*2);
    }

    @Override
    public void add(int pos, int data) {
        //1.满了
        if(isFull()) {
            resize();
        }
        //2.位置是否合法
        if(pos<0 || pos>usedSize) {
            //return;
            throw new PosOutOfException("pos位置不合法");
        }

        for (int i = usedSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        //存放数据
        elem[pos] = data;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        if(isFull()) {
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //判断数组内是否有值
    @Override
    public Boolean isEmpty() {
        return usedSize == 0;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if(pos<0 || pos>usedSize) {
            //return;
            throw new PosOutOfException("pos位置不合法");
        }
        if(isEmpty()) {
            return -1;
        }
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        if(pos<0 || pos>usedSize) {
            //return;
            throw new PosOutOfException("set方法中pos位置不合法");
        }
        if(isEmpty()) {
            return;
        }
        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            return;
        }
        for (int i = index; i < usedSize-1; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;
        regain();
    }

    @Override
    public void removeAllVal(int toAllRemove) {
        for (int i = 0; i < elem.length; i++) {
            remove(toAllRemove);
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }

    @Override
    public void regain() {
        for (int i = usedSize; i < elem.length; i++) {
            elem[i] = 0;
        }
    }
}
